package com.example.ucf.graphics;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.List;

public class Shape {
	private List<Point> points;
	// Cria buffers
	ShortBuffer indexBuffer;
	FloatBuffer vertexBuffer;
	
	public Shape(List<Point> points) {
		this.points = points;
		this.points.add(this.points.get(0));
		
		// Transforma num vetor auxiliar para criacao do buffer
		float pointsArray[] = new float[points.size()*2];
		short indicesArray[] = new short[points.size()];
		
		for(short i=0; i<indicesArray.length; i++){
			indicesArray[i] = i;
			pointsArray[i*2] = points.get(i).x;
			pointsArray[i*2+1] = points.get(i).y;
		}
		
		ByteBuffer vb = ByteBuffer.allocateDirect(indicesArray.length * 2 * 4);
	    vb.order(ByteOrder.nativeOrder());
	    vertexBuffer = vb.asFloatBuffer();
	 
	    // short has 2 bytes
	    ByteBuffer ib = ByteBuffer.allocateDirect(indicesArray.length * 2);
	    ib.order(ByteOrder.nativeOrder());
	    indexBuffer = ib.asShortBuffer();
	 
	    vertexBuffer.put(pointsArray);
	    indexBuffer.put(indicesArray);
	 
	    vertexBuffer.position(0);
	    indexBuffer.position(0);
	}
	
	public void draw(float red, float green, float blue){
	}
	
	public FloatBuffer getVertexBuffer(){
		return vertexBuffer;
	}
	public ShortBuffer getIndexBuffer(){
		return indexBuffer;
	}
	public int getNVertices(){
		return points.size();
	}
}
